package com.sctvcloud.qingchuan.ui.utils;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SpanUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.Utils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sctvcloud.qingchuan.R;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * @author suming
 * @date 2020/8/12 16:58
 */
public class PlaybackDataViewHelper implements BaseQuickAdapter.OnItemClickListener {
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

    private DateAdapter mAdapter;
    private OnPlaybackSelectedDateListener mOnPlaybackSelectedDateListener;

    public interface OnPlaybackSelectedDateListener {
        void onDateSelect(String year, String month, String dayOfMonth);

        void onSelectDialog();
    }

    public void setOnPlaybackSelectedDateListener(OnPlaybackSelectedDateListener listener) {
        this.mOnPlaybackSelectedDateListener = listener;
    }

    public PlaybackDataViewHelper(@NonNull Activity activity) {
        RecyclerView recyclerView = activity.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(activity, 8));
        mAdapter = new DateAdapter();
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);

        List<Calendar> intervalDateList = getIntervalDateList(7);
        List<DateEntry> dates = new ArrayList<>();
        for (int i = 0; i < intervalDateList.size(); i++) {
            dates.add(new DateEntry(
                    intervalDateList.get(i),
                    TextUtils.equals(TimeUtils.date2String(getToday().getTime(), FORMAT), TimeUtils.date2String(intervalDateList.get(i).getTime(), FORMAT))
            ));
        }
        Collections.reverse(dates);
        dates.add(new DateEntry(null, false));
        mAdapter.setNewData(dates);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        DateEntry item = mAdapter.getItem(position);
        List<DateEntry> data = mAdapter.getData();
        int size = mAdapter.getItemCount();
        if (item != null) {
            if (item.calendar != null) {
                for (int i = 0; i < size; i++) {
                    DateEntry entry = data.get(i);
                    entry.isSelected = entry.calendar == item.calendar;
                }
                mAdapter.notifyDataSetChanged();

                if (mOnPlaybackSelectedDateListener != null) {
                    String format = TimeUtils.date2String(item.calendar.getTime(), FORMAT);
                    String[] child = format.split("-");
                    mOnPlaybackSelectedDateListener.onDateSelect(child[0], child[1], child[2]);
                }
            } else {
                if (mOnPlaybackSelectedDateListener != null)
                    mOnPlaybackSelectedDateListener.onSelectDialog();
            }

        }
    }

    private static class DateAdapter extends BaseQuickAdapter<DateEntry, BaseViewHolder> {

        public DateAdapter() {
            super(R.layout.item_date);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, DateEntry item) {
            if (item.calendar != null) {
                helper.setText(R.id.date, new SpanUtils()
                        .append(getMonth(item.calendar))
                        .setFontSize(10, true)
                        .appendLine()
                        .append(getDayOfMonth(item.calendar))
                        .setFontSize(18, true)
                        .create());
                helper.setTextColor(R.id.date, item.isSelected ? ContextCompat.getColor(Utils.getApp(), R.color.white) :
                        ContextCompat.getColor(Utils.getApp(), R.color.colorGray99));
            }
            helper.setGone(R.id.date, item.calendar != null);
            helper.setGone(R.id.image, item.calendar == null);
            helper.setBackgroundColor(R.id.root, item.isSelected ? Color.parseColor("#01ABE3") :
                    Color.parseColor("#F6F6F6"));
        }
    }

    private static class DateEntry {
        public Calendar calendar;
        public boolean isSelected;

        public DateEntry(Calendar calendar, boolean isSelected) {
            this.calendar = calendar;
            this.isSelected = isSelected;
        }
    }

    private static List<Calendar> getIntervalDateList(int intervals) {
        List<Calendar> dates = new ArrayList<>();
        for (int i = 0; i < intervals; i++) {
            dates.add(backOfScale(i));
        }
        return dates;
    }

    private static Calendar backOfScale(int scale) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - scale);
        return calendar;
    }

    private static Calendar aheadOfScale(int scale) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + scale);
        return calendar;
    }

    private static String getMonth(Calendar calendar) {
        return MessageFormat.format("{0}月", calendar.get(Calendar.MONTH) + 1);
    }

    private static String getDayOfMonth(Calendar calendar) {
        return MessageFormat.format("{0}", calendar.get(Calendar.DAY_OF_MONTH));
    }

    private static Calendar getToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(TimeUtils.getNowDate());
        return calendar;
    }
}
